package com.webwork.lims.transport.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webwork.lims.transport.dao.ZoneDao;
import com.webwork.lims.transport.dataobject.Zone;
import com.webwork.lims.transport.service.ZoneService;
import com.webwork.lims.util.Paging;
import com.webwork.lims.util.StrManager;

@Service
public class ZoneServiceImpl implements ZoneService {

	@Autowired
	private ZoneDao zoneDao;

	@Override
	public List<Zone> query(Zone queryObj) {
		return this.query(queryObj, new Paging(0, 0));
	}

	@Override
	public int count(Zone queryObj) {
		return zoneDao.count(queryObj);
	}

	@Override
	public List<Zone> query(Zone queryObj, Paging paging) {
		return zoneDao.query(queryObj, paging.getCurrPage(), paging.getPageSize());
	}

	@Override
	public Zone get(int id) {
		Zone queryObj = new Zone();
		queryObj.setZoneCode(id);
		List<Zone> zones = this.query(queryObj);
		if (zones != null && zones.size() > 0) {
			if (zones.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return zones.get(0);
			}
		} else {
			System.out.println("找不到该地区!");
		}
		return null;
	}
	
	@Override
	public Zone getByDelivery(String user) {
		Zone queryObj = new Zone();
		queryObj.setDelivery(user);
		List<Zone> zones = this.query(queryObj);
		if (zones != null && zones.size() > 0) {
			if (zones.size() > 1) {
				System.out.println("存在不止一条记录!");
			}
			return zones.get(0);
		} else {
			System.out.println("找不到该地区!");
		}
		return null;
	}

	@Override
	public void insert(Zone zone) {
		zoneDao.insert(zone);
	}

	@Override
	public void update(Zone zone) {
		zoneDao.update(zone);
	}

	@Override
	public void save(Zone zone) {
		if (this.get(zone.getZoneCode()) != null) {
			this.update(zone);
		} else {
			this.insert(zone);
		}
	}

	@Override
	public void delete(int id) {
		zoneDao.delete(id);
	}
	
	@Override
	public List<String> queryCountryByGroup() {
		return zoneDao.queryCountryByGroup();
	}
	
	@Override
	public List<String> queryCityByGroup(String province) {
		return zoneDao.queryCityByGroup(province);
	}
	
	@Override
	public List<String> queryProvinceByGroup(String country) {
		return zoneDao.queryProvinceByGroup(country);
	}

	@Override
	public String toChinese(String wayLine) {
		String ret = "";
		String[] zones = wayLine.split("-");
		for (String zoneCode : zones) {
			if (!StrManager.isEmpty(zoneCode)) {
				Zone zone = this.get(Integer.parseInt(zoneCode));
				ret += zone.getCity() + "-";
			}
		}
		if (!StrManager.isEmpty(ret)) {
			ret = ret.substring(0, ret.length() - 1);
		}
		return ret;
	}
	
}
